#include <UTILS/SBuffer.h>
#include <PECAgent/PEContext.h>
#include <DPS/PH/DpsFunctions.h>
#include <DPS/PH/DpsOPContext.h>
#include "UDOP_ReadPARDUMP.h"
#include <stdio.h>

SPC_NAMESPACE_USE;
UTILS_NAMESPACE_USE;
using namespace DPS;
using namespace std;
using namespace estd;

/*
 * Function used to initialize the operator
 */
void UDOP_ReadPARDUMP::initializeOperator() 
{
  // Create a user defined metric
  // opm.createMetric("intuples","Number of ingested tuples");
}

/*
 * Function used to finalize the operator
 */
void UDOP_ReadPARDUMP::finalizeOperator() 
{
}

/*
 * Function used to process command line arguments
 */
void UDOP_ReadPARDUMP::processCmdArgs(const string &args) 
{
  //SPCDBG(L_INFO, "Process command line arguments...", "dpsop");
  // ArgContainer arg(args); // include "ArgContainer.h"
  // arg.argc and arg.argv are now available for use
  // do other initialization here
}


/*
 * Process function for this source operator
 */
void UDOP_ReadPARDUMP::process()
{
  //SPCDBG(L_DEBUG, "Process...", "dpsop");
  /*
   * while(!(peHandle.getShutdownRequested())) {
   *   OPort0_t otuple; submit0(otuple);
   * }
   */
   //std::string theName = peContext.getName();
	int info = 0;
	int i = 1;
	int numpar = 0;
	char str1[50];
	char str2[100];
	char parfile[180];
	double mass = 0.0;
	float tlat = 0.0;
	float tlon = 0.0;
	const DpsOPContext &context = this->getContext();
	const std::string &args = context.getCmdArguments();
	//SPCDBG(L_INFO, "args is " << args, "dpsop");
	strcpy(str1, args.c_str());
	info = std::sprintf(parfile, "/home/shiming1/InfoSphereStreams/letkf-spade/hysplit/data/test%c", str1[11]);
	OPort0_t otuple;
	FILE *fp;
	fp = fopen(parfile, "r");
	fgets(str2, 100, fp);
	fgets(str2, 100, fp);
	fgets(str2, 100, fp);
	fgets(str2, 100, fp);
	info = std::fscanf(fp,"%d", &numpar);
	fgets(str2, 100, fp);

	for (i=0; i<numpar; i++)
	{
		info = std::fscanf(fp,"%lE", &mass);
		info = std::fscanf(fp,"%f", &tlat);
		info = std::fscanf(fp,"%f", &tlon);
		fgets(str2, 100, fp);
		fgets(str2, 100, fp);
		otuple._mass = mass;
		otuple._tlat = tlat;
		otuple._tlon = tlon;
		
		submit0(otuple);
	}
	fclose(fp);
	//i = std::fprintf(fp, "%d", cnt);
//	cnt = cnt + 1;
}


/*
 * Function used to notify the operator about the readiness of all ports (this
 * is useful when submitting tuples from within a context other than the process
 * functions of the operator).
 */ 
/*
void UDOP_ReadPARDUMP::allPortsReady() 
{
} 
*/

/*
 * Terminate function, called asynchronously
 */
/*
void UDOP_ReadPARDUMP::prepareToTerminate() 
{
}
*/

/*
 * Function used to update performance counters
 */
/*
static void updatePerformanceCounters(DpsOPMetrics & opm) 
{ 
  static long long ntuples = 0;
  // This is not the ideal way of using performance counters
  // Ideally one should store the result from getMetricByName, 
  // in a class member variable. For instance:
  //   DpsMetric & m = opm.getMetricByName("intuples");
  // m could be made a member variable of UDOP_ReadPARDUMP
  // Then, the following is a more efficient way to use the counter:
  //   m.setValue(ntuples++);
  opm.getMetricByName("intuples").setValue(ntuples++);
}
*/

/*
 * Checkpointing function for saving state, called asynchronously 
 */
/*
void UDOP_ReadPARDUMP::getCheckpoint(SBuffer & checkpoint) 
{
  SPCDBG(L_DEBUG, "Getting operator state...", "dpsop");
}
*/

/*
 * Checkpointing function for restoring state, called asynchronously
 */
/*
void UDOP_ReadPARDUMP::restoreCheckpoint(SBuffer & checkpoint) 
{
  SPCDBG(L_DEBUG, "Restoring operator state...", "dpsop");
}
*/


